package com.sheng.sign.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.sheng.sign.entity.Student;

public class StudentDaoImpl implements StudentDao {
	
	 private SessionFactory sessionFactory;
	 
	 
		public SessionFactory getSessionFactory() {
			return sessionFactory;
		}
		public void setSessionFactory(SessionFactory sessionFactory) {
			this.sessionFactory = sessionFactory;
		}

	@Override
	public boolean add(Student student) {
		try {
			student.setIsDelete(0);
			sessionFactory.getCurrentSession().save(student);
			System.out.println("==来到了DaoImpl=="+student);
			return true;
		} catch (HibernateException e) {
			return false;
		}

	}

	@Override
	public boolean edit(Student student) {
		try {
			student.setIsDelete(0);
			sessionFactory.getCurrentSession().update(student);
			System.out.println("==来到了DaoImpl=="+student);
			return true;
		} catch (HibernateException e) {
			return false;
		}
	}

	@Override
	public boolean delete(Student student) {
		try {
			student.setIsDelete(1);
			sessionFactory.getCurrentSession().update(student);
			System.out.println("==来到了DaoImpl=="+student);
			return true;
		} catch (HibernateException e) {
			return false;
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<Student> findAllList() {
		String hql = "from Student where 1=1 and is_delete=0";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);  
        List list = query.list();  
        return list;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Student findById(Student student) {
		Query query = sessionFactory.getCurrentSession().createQuery("from Student where id='"+student.getId()+"'");  
		List list = query.list();  
		if (null != list && 0 < list.size()) {
			student = (Student) list.get(0);
		}
		return student;
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<Student> listByClass(Student student) {
		System.out.println("考勤选择的班级id=="+student.getClassesName());
		String hql = "from Student where classes_name like '%"+student.getClassesName()+"%' and is_delete=0 ";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);  
        List list = query.list();  
        return list;
	}

}
